/**
 * @author rotuka
 */
window.sls ? null : window.sls = {};
sls.Modal = Class.create();
sls.Modal.prototype = {
	yPos : 0, xPos : 0,
	initialize: function() {
		sls.modal = this;
		if ($('#slsModalOverlay').size() < 1) {
			$(document.body || document.documentElement).append(
				'<div id="slsModalOverlay"></div>'+
				'<div id="slsModal"></div>'
			);
		} else {
			
		}
		var opa = 0.3;
		var overlay = $('#slsModalOverlay').css({
			'-moz-user-select':opa, 'opacity':opa, '-khtml-opacity': opa, 'filter': "alpha(opacity=" + opa * 100 + ")",
			width: sls.body.offsetWidth, height: window.innerHeight /*sls.body.offsetHeight */
		}).get(0);
		$('#slsModal').add('#slsModalOverlay').appendTo('body')
	},
	
	show: function (content) {
		$('#slsModal').show().append(content);
		this.activate();
		$('#slsModal').attr('class', "done");
		this.actions();
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if ($.browser.msie){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0, 0);
			this.hideSelects('hidden');
		}
		this.centrate();
		this.displayLightbox("block");
	},
	
	centrate: function() { // ((-:
		var wi = 0, he = 0;
		$('#slsModal > *').each(function() {
			wi += this.offsetWidth;
			he += this.offsetHeight;
		});
		$('#slsModal').width(wi).height(he).css({
			'margin-top': (- he / 2),
			'margin-left': (- wi / 2) 
		}).hide().show();
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		$('#slsModal').empty();
		if ($.browser.msie){
			this.setScroll(0, this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		this.displayLightbox("none");
	},
	

	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		$('body, html').css({
			'height': height,
			'overflow': overflow
		});
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		$('select').css('visibility', visibility);
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	displayLightbox: function(display){
		$('#slsModalOverlay').css('display', display);
		$('#slsModal').css('display', display);
	},
	
	pull: function () {
		$('input', '#slsModalOverlay').css('z-index', 8000);
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		var thiz = this;
		$('.slsModalAction').each(function(i) {
			$(this).click(function(){
				(thiz[this.rel])();
				return false;
			})
		});
	}
}
$(function() {
	new sls.Modal();
});
